<?php  
	
	namespace Home\Controller;
	use Think\Controller;
	
	class AffairController extends AllowController{
        
        // 投诉建议的的列表查询
        public function indexs(){
            //实例化
            $mod = M('complain');
            $mods = M('users');
            //总数
            $tot=$mod->where("type=1")->Count();

            //分页条数
            $nums = 5;
            $page = new \Think\Page($tot,$nums);

            $list = $mod->limit($page->firstRow,$page->listRows)->order('sendtime desc')->where("type=1")->select();
            //遍历 把时间戳转换日期
            foreach($list as $key=>$value){
                $list[$key]['sendtime'] = date("Y-m-d H:i:s",$value['sendtime']);

                //sender为0就是匿名发送
                if ($list[$key]['sender'] == 0) {
                   $list[$key]['sender'] = "匿名";
                }else{
                    //通过sender的用户id去关联users查询用户姓名
                   $name = $mods->where("id={$list[$key]['sender']}")->find();
                   $list[$key]['sender'] = $name["name"];
                }
            }
            if(empty($_GET['p'])){
                $p = 1;
            }else{
                $p = $_GET['p'];
            }
            //分配数据
            $this->assign('list',$list);
            $this->assign('curr',$p);
            $this->assign('page',ceil($tot/$nums));
            $this->assign('total',$tot);

            //导入模版
            $this->display("Complain/complains");
        }

        //显示look模块
        public function look(){
            $a = I('get.id');

            //实例化
            $mod = M('complain');
            $mods = M("users");           
            
            //查询单条语句
            $list=$mod->find($a);

            //获取发送人id
            $b = $list["sender"];

            //查询users下$b的name值
            $lists=$mods->field("name")->find($b);

            //获取sender的值进行判断
            if($b == 0){
            	 $list["sender"] = '匿名';
            }else{
            	 $list["sender"] = $lists['name'];
            }
            
            //分配数据
            $this->assign('list',$list);
            $this->display('Complain/look');
        }


        //
        public function update(){
            //实例化
            $mod = M('complain');

            $data = array();
            $data['id'] = $_POST['id'];
            $data['status'] = 1;
            
            //修改数据
            if($mod->save($data)){
                echo "1";
            }else{
                echo "0";
            }
        }

        // 删除
        public function delete()
        {
            $mod = M('complain');
            //修改数据
            if($mod->delete($_GET['id'])){
                echo "1";
            }else{
                echo "0";
            }
        }
    }
?>
